package ogdkp.providers;

import ogdkp.om.Player;

import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerComparator;
import org.eclipse.swt.SWT;

public class DKPComparator extends ViewerComparator {

	private int column;

	private static final int DESCENDING = 1;

	private int direction;

	public DKPComparator() {
		column = 0;
		direction = DESCENDING;
	}

	public int getDirection() {
		return direction == 1 ? SWT.DOWN : SWT.UP;
	}

	public void setColumn(final int column) {
		if (column == this.column) {
			direction = 1 - direction;
		} else {
			this.column = column;
			direction = DESCENDING;
		}
	}

	@Override
	public int compare(final Viewer viewer, final Object e1, final Object e2) {
		if (!(e1 instanceof Player)) {
			return 0;
		}
		final Player p1 = (Player) e1;
		final Player p2 = (Player) e2;
		int rc = 0;
		switch (column) {
		case 0:
			rc = p1.getName().compareTo(p2.getName());
			break;
		case 1:
			rc = p1.getClassName().compareTo(p2.getClassName());
			break;
		case 2:
			rc = new Integer(p1.getStartingDKP()).compareTo(new Integer(p2.getStartingDKP()));
			break;
		case 3:
			rc = new Integer(p1.getSpentDKP()).compareTo(new Integer(p2.getSpentDKP()));
			break;
		case 4:
			rc = new Integer(p1.getCurrentDKP()).compareTo(new Integer(p2.getCurrentDKP()));
			break;
		}

		if (direction == DESCENDING) {
			rc = -rc;
		}
		return rc;

	}
}
